www.gusucode.com > VC++ ASP文件上传组件源代码+调用示例-源码程序 > VC++ ASP文件上传组件源代码+调用示例-源码程序/code/aspup_src/MyRequest/Item.cpp

    //Download by http://www.NewXing.com
// This is a part of the Active Template Library. 
// Copyright (C) 1995-1999 Microsoft Corporation
// All rights reserved. 
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.

// Item.cpp : Implementation of CMapCollection
#include "stdafx.h"
#include "MyRequest.h"
#include "Item.h"
#include <string>
using namespace std;

/////////////////////////////////////////////////////////////////////////////
// CItem

CItem::CItem()
{
	m_lSize = 0;
	m_bFile = FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// ISupportsErrorInfo interface implementation

STDMETHODIMP CItem::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IItem
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// IItem interface implementation


STDMETHODIMP CItem::get_Value(VARIANT* pValue)
{
	CComVariant var = m_varValue;
	return var.Detach(pValue);
}

STDMETHODIMP CItem::IsFile(BOOL *pVal)
{
	*pVal = m_bFile;
	return S_OK;
}

STDMETHODIMP CItem::get_FileName(BSTR *pVal)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.FileName"), _T("Item is not a file"), IID_IItem);

	CComBSTR str = m_bstrFileName;
	*pVal = str.Detach();
	return S_OK;
}

STDMETHODIMP CItem::get_FilePath(BSTR *pVal)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.FilePath"), _T("Item is not a file"), IID_IItem);

	CComBSTR str = m_bstrFilePath;
	*pVal = str.Detach();
	return S_OK;
}

STDMETHODIMP CItem::get_FileExt(BSTR *pVal)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.FileExt"), _T("Item is not a file"), IID_IItem);

	CComBSTR str = m_bstrFileExt;
	*pVal = str.Detach();

	return S_OK;
}

STDMETHODIMP CItem::get_ContentType(BSTR *pVal)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.ContentType"), _T("Item is not a file"), IID_IItem);

	CComBSTR str = m_bstrContentType;
	*pVal = str.Detach();
	return S_OK;
}

STDMETHODIMP CItem::get_TotalBytes(long *pVal)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.TotalBytes"), _T("Item is not a file"), IID_IItem);

	*pVal = m_lSize;
	return S_OK;
}

STDMETHODIMP CItem::GetChunk(long offset, long length, VARIANT* pChunk)
{
	if(m_bFile == FALSE)
		return Error(_T("Item.GetChunk()"), _T("Item is not a file"), IID_IItem);

	// ** Validating arguments
	if(offset < 0 || offset >= m_lSize) 
		return Error(_T("Item.GetChunk()"), _T("Invalid offset"), IID_IItem, E_INVALIDARG);

	if(length < 0) 
		return Error(_T("Item.GetChunk()"), _T("Invalid length"), IID_IItem, E_INVALIDARG);

	if(length > m_lSize - offset) length = m_lSize - offset;

	CComVariant var = m_varValue;
	var.ChangeType(VT_ARRAY | VT_UI1);

	SAFEARRAY* pSaveArray = var.parray;
	BYTE HUGEP *pData;

	// ** Get a pointer to the elements of the array.
	SafeArrayAccessData(pSaveArray, (void HUGEP* FAR*)&pData);

	long lLBound, lUBound;
	// ** One dimensional array. Get the bounds for the array.
	SafeArrayGetLBound(pSaveArray, 1, &lLBound);
	SafeArrayGetUBound(pSaveArray, 1, &lUBound);

	// ** Get the count of elements
	long cElements = lUBound-lLBound + 1;

	if(offset > cElements) 
		return Error(_T("Item.GetChunk()"), _T("Invalid offset"), IID_IItem, E_INVALIDARG);

	if(length > cElements - offset) length = cElements - offset;

	// ** Get a pointer to the elements of the array.
	SAFEARRAY* pChunkSafeArray = SafeArrayCreateVector(VT_UI1, 0, length);

	BYTE HUGEP *pChunkData;
	SafeArrayAccessData(pChunkSafeArray, (void HUGEP* FAR*)&pChunkData);

	// ** Get the elements of the array
	memcpy(pData + offset, pChunkData, length);
	//Decrement the access count for the array.
	SafeArrayUnaccessData(pChunkSafeArray);

	//Decrement the access count for the array.
	SafeArrayUnaccessData(pSaveArray);

	VARIANT var2;
	var2.vt = VT_ARRAY | VT_UI1;
	var2.parray = pChunkSafeArray;
	*pChunk = var2;

	return S_OK;
}

BOOL CItem::isFile() {
	return m_bFile;
}

HRESULT CItem::SetItem(CBuffer bContent)
{
	m_bFile = FALSE;
	CComBSTR bstrValue = bContent;
	m_varValue = bstrValue;

	return S_OK;
}

HRESULT CItem::SetItem(CBuffer bContent, CComBSTR bstrFileName, CComBSTR bstrContentType)
{
	m_bFile = TRUE;
	m_bstrFileName = _T("");
	m_bstrFileExt = _T("");
	m_bstrFilePath = _T("");

	wstring wstr = bstrFileName;
	if(wstr != L"") {
		int i = wstr.rfind(L"\\");
		if(i != -1) {
			wstring wstrFileName = wstr.substr(i+1);
			wstring wstrFilePath = wstr.substr(0, i+1);
			m_bstrFileName = wstrFileName.c_str();
			m_bstrFilePath = wstrFilePath.c_str();
			i = wstrFileName.rfind(L".");
			if(i != -1) {
				wstring wstrFileExt = wstrFileName.substr(i+1);
				m_bstrFileExt = wstrFileExt.c_str();
			} 
		} else {
			m_bstrFileName = wstr.c_str();
			i = wstr.rfind(L".");
			if(i != -1) {
				wstring wstrFileExt = wstr.substr(i);
				m_bstrFileExt = wstrFileExt.c_str();
			} 
		}
	}

	m_bstrContentType = bstrContentType;
	int nLength = bContent.m_nSize;

	if(nLength != 0) {
		SAFEARRAY* pSafeArray = SafeArrayCreateVector(VT_UI1, 0, nLength);

		LPBYTE lpData;
		SafeArrayAccessData(pSafeArray, (void HUGEP* FAR*)&lpData);

		LPBYTE lpBuffer = bContent.m_lpBuffer;
		memcpy(pSafeArray->pvData, lpBuffer, nLength);
		SafeArrayUnaccessData(pSafeArray);

		VARIANT v;
		v.vt = VT_ARRAY | VT_UI1;
		v.parray = pSafeArray;

		CComVariant vValue;
		vValue.Attach(&v);
		m_varValue = vValue;
		m_lSize = nLength;
	} else {
		CComBSTR bstr = _T("");
		m_varValue = bstr;
		m_lSize = 0;
	}

	return S_OK;
}

CComVariant CItem::GetValue() {
	return m_varValue;
}

CComBSTR CItem::GetFileName()
{
	return m_bstrFileName;
}